package org.meb.conquest.web;

import java.util.regex.Pattern;

import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang.ArrayUtils;
import org.meb.conquest.web.ctrl.CardSearchCtrl;
import org.meb.conquest.web.ctrl.CommonCtrl;
import org.meb.conquest.web.services.WebContext;

// @FacesConverter("cardTextConverter")
// @RequestScoped
@Named
@RequestScoped
public class CardTextConverter implements Converter {

	private static final String[] EFFECTS_EN = { "Lost", "Response", "Shadow", "When Revealed",
			"When Sacked", "Quest Action", "Forced", "Combat Action", "Refresh Action",
			"Travel Action", "Setup", "Planning Action", "Encounter Action", "Travel", "Action", "Riddle" };
	private static final String[] EFFECTS_DE = (String[]) ArrayUtils.addAll(new String[] {
			"Reaktion", "Schatten", "Wenn aufgedeckt", "Beim Einsacken", "Abenteueraktion",
			"Erzwungen", "Kampfaktion", "Beim Aufdecken", "Auffrischungsaktion", "Reiseaktion",
			"Vorbereitung", "Planungsaktion", "Begegnungsaktion", "Reise", "Aktion" }, EFFECTS_EN);
	private static final String[] EFFECTS_PL = (String[]) ArrayUtils.addAll(new String[] {
			"Zagubieni", "Odpowiedź", "Cień", "Po odkryciu", "Pochwyć w worek", "Akcja Wyprawy", "Wymuszony", "Akcja Walki",
			"Akcja Odpoczynku", "Reiseaktion", "Przygotowanie", "Akcja Planowania",
			"Begegnungsaktion", "Podróż", "Akcja", "Zagadka" }, EFFECTS_EN);

	@Inject
	private CardSearchCtrl cardSearchCtrl;

	private static final Pattern PATTERN_VICTORY_DE = Pattern.compile("(Sieg [0-9]\\.)");
	private static final Pattern PATTERN_VICTORY_EN = Pattern.compile("(Victory [0-9]\\.)");
	private static final Pattern PATTERN_VICTORY_PL = Pattern.compile("(Zwycięstwo [0-9]\\.)");

	@Inject
	private WebContext user;

	@Inject
	private CommonCtrl commonCtrl;

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object textIn) {
		String textOut = (String) textIn;
//		String contextPath = arg0.getExternalContext().getRequestContextPath();
//		String commonImagePath = contextPath + commonCtrl.getCommonImagePath();
//		String sphereImagePath = contextPath + commonCtrl.getSphereImagePath();
//
//		textOut = textOut.replace("${attack}", "<img src=\"" + commonImagePath
//				+ "attack_16x16.png\" class=\"card-stat-icon\" />");
//		textOut = textOut.replace("${defense}", "<img src=\"" + commonImagePath
//				+ "defense_16x16.png\" class=\"card-stat-icon\" />");
//		textOut = textOut.replace("${willpower}", "<img src=\"" + commonImagePath
//				+ "willpower_16x16.png\" class=\"card-stat-icon\" />");
//		textOut = textOut.replace("${threat}", "<img src=\"" + commonImagePath
//				+ "threat_16x16.png\" class=\"card-stat-icon\" />");
//
//		textOut = textOut.replace("${leadership}", "<img src=\"" + sphereImagePath
//				+ "leadership-small-bw.png\" class=\"card-text-icon\" />");
//		textOut = textOut.replace("${tactics}", "<img src=\"" + sphereImagePath
//				+ "tactics-small-bw.png\" class=\"card-text-icon\" />");
//		textOut = textOut.replace("${spirit}", "<img src=\"" + sphereImagePath
//				+ "spirit-small-bw.png\" class=\"card-text-icon\" />");
//		textOut = textOut.replace("${lore}", "<img src=\"" + sphereImagePath
//				+ "lore-small-bw.png\" class=\"card-text-icon\" />");
//		textOut = textOut.replace("${baggins}", "<img src=\"" + sphereImagePath
//				+ "baggins-small-bw.png\" class=\"card-text-icon\" />");
//
//		textOut = textOut.replace("[b]", "<b>").replace("[/b]", "</b>");
//		textOut = textOut.replace("[i]", "<i>").replace("[/i]", "</i>");
//		textOut = textOut.replace("[ul]", "<ul>").replace("[/ul]", "</ul>");
//		textOut = textOut.replace("[li]", "<li>").replace("[/li]", "</li>");
//		textOut = textOut.replace("[questStage]", "<span class=\"card-quest-stage\">").replace(
//				"[/questStage]", "</span>");
//		if (cardSearchCtrl.isShowCorrections()) {
//			textOut = textOut.replace("[error]", "<b><span class=\"error-text\">").replace(
//					"[/error]", "</span></b>");
//		} else {
//			textOut = textOut.replace("[error]", "").replace("[/error]", "");
//		}
//		textOut = textOut.replace("[trait]", "<span class=\"test\">")
//				.replace("[/trait]", "</span>");
//		textOut = textOut.replace("${error}", "");
//		for (String effect : getEffects((String) textIn)) {
//			textOut = textOut.replace(effect + ":", "<b>" + effect + ":</b>");
//		}
//
//		String[] textOutLines = textOut.split("\n");
//		textOut = "";
//		for (String textOutLine : textOutLines) {
//			textOut += "<p>" + textOutLine + "</p>";
//		}
//		textOut = makeVictoryBold(textOut);
		return textOut;
	}

	private String[] getEffects(String textIn) {
		if (user.getLanguage().equals("de")) {
			return EFFECTS_DE;
		} else if (user.getLanguage().equals("en")) {
			return EFFECTS_EN;
		} else if (user.getLanguage().equals("pl")) {
			return EFFECTS_PL;
		} else {
			return new String[] {};
		}
	}

	private String makeVictoryBold(String textIn) {
		if (user.getLanguage().equals("de")) {
			return PATTERN_VICTORY_DE.matcher(textIn).replaceAll("<b>$1</b>");
		} else if (user.getLanguage().equals("en")) {
			return PATTERN_VICTORY_EN.matcher(textIn).replaceAll("<b>$1</b>");
		} else if (user.getLanguage().equals("pl")) {
			return PATTERN_VICTORY_PL.matcher(textIn).replaceAll("<b>$1</b>");
		} else {
			return textIn;
		}
//		return textIn;
	}
}
